package com.siblings.loan.ui.loan.fm

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import com.siblings.loan.R
import com.siblings.loan.databinding.FmLoanLayerBinding
import com.siblings.loan.ui.loan.impl.SibLoanLayerPresenterImpl
import com.siblings.loan.ui.loan.view.ISibLoanLayerView
import com.siblings.loan.utils.event.SibEventData
import com.siblings.mvp.MvpFm
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.json.JSONObject

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/8 18:12
 * @Annotation：贷款额度
 */
class SibLoanLayerFm: MvpFm<ISibLoanLayerView,SibLoanLayerPresenterImpl>(),ISibLoanLayerView {
    lateinit var binding: FmLoanLayerBinding

    //未授信
    private var uncreditedFm: SibLoanUncreditedFm? = null
    //贷款额度
    private var limitFm: SibLoanLimitFm? = null
    //通用
    private var commonFm: SibLoanCommonFm? = null

    override fun getSibRootView(inflater: LayoutInflater): View {
        binding = FmLoanLayerBinding.inflate(inflater)
        return binding.root
    }

    override fun createMvpPresenter(): SibLoanLayerPresenterImpl {
        return SibLoanLayerPresenterImpl(context)
    }

    @SuppressLint("CommitTransaction")
    override fun initSibView() {
        rootViewBinding.containerLayout.setBackgroundColor(Color.parseColor("#00000000"))

        EventBus.getDefault().register(this)
        initSibRefreshLayout()
    }

    override fun initSibListener() {
    }

    override fun initSibData() {
        showSibLoading()
        mvpPresenter.loadSibProcessAction()
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    fun onRefreshProcess(data: SibEventData){
        when(data.eventType){
            SibEventData.TYPE.SIB_PROCESS_REFRESH -> {
                mvpPresenter.loadSibProcessAction()
            }
            SibEventData.TYPE.SIB_SHOW_GUIDE_DIALOG -> {
                if (limitFm != null && limitFm!!.isAdded){
                    limitFm!!.showGuideDialog()
                }
            }
            else -> {

            }
        }
    }

    /* 初始化刷新 */
    private fun initSibRefreshLayout() {
        //禁止加载
//        binding.refreshLayout.setEnableLoadMore(false)
//        binding.refreshLayout.setEnableAutoLoadMore(false)
//        binding.refreshLayout.setOnRefreshListener {
//            mvpPresenter.loadSibProcessAction()
//            //发送刷新流程通知
////            EventBus.getDefault().post(SibEventData(SibEventData.TYPE.SIB_PROCESS_REFRESH))
//        }
//        binding.refreshLayout.setOnLoadMoreListener {
//            binding.refreshLayout.finishLoadMore()
//        }
    }

    /**
     * 隐藏贷款所有 Page
     */
    override fun hideSibAllLoanPage() {
        if (uncreditedFm != null) {
            childFragmentManager
                .beginTransaction()
                .hide(uncreditedFm!!)
                .commitAllowingStateLoss()
        }
        if (limitFm != null) {
            childFragmentManager
                .beginTransaction()
                .hide(limitFm!!)
                .commitAllowingStateLoss()
        }
        if (commonFm != null) {
            childFragmentManager
                .beginTransaction()
                .hide(commonFm!!)
                .commitAllowingStateLoss()
        }
    }

    /**
     * 未授信
     */
    override fun showSibUncreditedPage() {
        if (uncreditedFm == null) {
            uncreditedFm = SibLoanUncreditedFm()
        }
        val transaction = childFragmentManager.beginTransaction()
        if (uncreditedFm!!.isAdded) {
            transaction.show(uncreditedFm!!)
        } else {
            transaction.add(R.id.contentLayout,uncreditedFm!!)
        }
        transaction.commitAllowingStateLoss()
    }

    /**
     * 贷款额度
     */
    override fun showSibLoanLimitPage() {
        if (limitFm == null) {
            limitFm = SibLoanLimitFm()
        }
        val transaction = childFragmentManager.beginTransaction()
        if (limitFm!!.isAdded) {
            transaction.show(limitFm!!)
            limitFm!!.onRefreshLoanLimitAction()
        } else {
            transaction.add(R.id.contentLayout,limitFm!!)
        }
        transaction.commitAllowingStateLoss()
    }

    /**
     * 审核中、放款中、已拒绝
     */
    override fun showSibLoanCommonPage(data: JSONObject) {
        if (commonFm == null) {
            commonFm = SibLoanCommonFm(data)
        }
        val transaction = childFragmentManager.beginTransaction()
        if (commonFm!!.isAdded) {
            transaction.show(commonFm!!)
            commonFm!!.refreshView(data)
        } else {
            transaction.add(R.id.contentLayout,commonFm!!)
        }
        transaction.commitAllowingStateLoss()
    }

    override fun hideSibLoading() {
        super.hideSibLoading()
//        binding.refreshLayout.finishRefresh()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}